var UserPet = require("UserPet");
var User = require("User");
var Data = require("../resources/scripts/data");
var UserTemplate = require("UserTemplate");
cc.Class({
    extends: cc.Component,
    properties: {
        m_users : [UserTemplate],
        cur_user : 0,
        ui_cur_skills : [cc.Node],
        pre_ui_skill : cc.Prefab,
        panel_skill : cc.Node,
        panel_msg : cc.Node,
        pre_msg : cc.Prefab,
        flag_defend : false,
        panel_skillContent : cc.Node,
        ui_scroll : cc.ScrollView,
    },
    // onLoad () {},

    save(){
        for(var i = 0;i<this.m_users.length;++i){
            cc.sys.localStorage.setItem('Seek-User'+i,JSON.stringify(this.m_users[i].user));
        }
    },

    read(){
        for(var i = 0;i<this.m_users.length;++i){
            this.m_users[i].user = JSON.parse(cc.sys.localStorage.getItem('Seek-User'+i));
            if(this.m_users[i].user == null){
                this.m_users[i].user = new User();
                var m_pet_ = new UserPet();
                m_pet_.PetIndex = 0;
                this.m_users[i].user.Pets.push(m_pet_);
            }
        }
    },

    loadPet(UserPetIndex,UserIndex){
        var s_pet = new Data.Pets[this.m_users[UserIndex].user.Pets[UserPetIndex].PetIndex];
        s_pet.m_FightScene = this;
        //console.log(s_pet.Blood);
        //s_pet.skill(0);
        //console.log(s_pet.Blood);
        cc.loader.loadRes('pet/'+s_pet.skeleton, dragonBones.DragonBonesAsset, (err, res) => {
            if (err) cc.error(err);
            this.m_users[UserIndex].render.dragonAsset = res;
            cc.loader.loadRes('pet/'+s_pet.texture, dragonBones.DragonBonesAtlasAsset, (err, res) => {
                if (err) cc.error(err);
                this.m_users[UserIndex].render.dragonAtlasAsset = res;
                //this.m_users[UserIndex].render.armatureName = this.m_users[UserIndex].render.getArmatureNames()[0];
                this.m_users[UserIndex].render.armatureName = s_pet.armature;
                this.m_users[UserIndex].render.playTimes = 0;
                //this.m_users[UserIndex].render.playAnimation(this.m_users[UserIndex].render.getAnimationNames(this.m_users[UserIndex].render.armatureName)[9],0);
                this.m_users[UserIndex].render.playAnimation("normal",0);
                this.m_users[UserIndex].render.addEventListener(dragonBones.EventObject.FRAME_EVENT, this.EventHandler, this);
            });
        });
        this.m_users[UserIndex].ui_blood.progress = 1.0;
        this.m_users[UserIndex].name.string = s_pet.name;
        this.m_users[UserIndex].cur_pet = s_pet;
    },

    playAniamtion(user,animName,time){
        //this.m_users[user].render.playAnimation(this.m_users[user].render.getAnimationNames(this.m_users[user].render.armatureName)[AnimIndex],time);
        this.m_users[user].render.playAnimation(animName,time);
    },

    outMsg(msgContent){
        var s_msg = cc.instantiate(this.pre_msg);
        s_msg.getComponent(cc.Label).string = msgContent;
        this.panel_msg.addChild(s_msg);
        this.ui_scroll.scrollToBottom(0);
    },

    EventHandler(event) {
        //if (event.type === dragonBones.EventObject.FADE_IN_COMPLETE) {
        //}
        //this.playAniamtion(this.getOtherUser(),9,0);
        if(event.name == "afterAttack"){
            //event.armature.display.playAnimation("normal",0);
            this.m_users[this.cur_user].render.playAnimation("normal",0);
            if(this.flag_defend)
                this.nextTurn(),this.flag_defend = false;
            else 
            this.flag_defend = true;
        }
        if(event.name == "onAttack"){
            //event.armature.display.playAnimation("normal",0);
            this.m_users[this.getOtherUser()].render.playAnimation("defend",1);
        }
        if(event.name == "afterDefend"){
            event.armature.display.playAnimation("normal",0);
            //this.m_users[this.getOtherUser()].render.playAnimation("defend",1);
            if(this.flag_defend)
                this.nextTurn(),this.flag_defend = false;
            else 
            this.flag_defend = true;
        }
            
        //console.log(event);
        //console.log(event.animationState);
        //console.log(event.armature);
    },

    chooseSkill(skillIndex){
        this.closeSkillLists();
        //console.log(skillIndex);
        this.setTarget();
        this.m_users[this.cur_user].cur_pet.skill(skillIndex);
        this.playAniamtion(this.cur_user,this.m_users[this.cur_user].cur_pet.skillContent[skillIndex].anim,1);
        //this.updateMsg();

        //this.setCurUser(this.getOtherUser());
        //this.outSkillLists();
    },

    nextTurn(){
        if(this.updateMsg()){
            this.setCurUser(this.getOtherUser());
            this.outSkillLists();
        }else {

        }
    },

    getOtherUser(){
        return Number(!Boolean(this.cur_user));
    },

    setTarget(){
        this.m_users[this.cur_user].cur_pet.target = this.m_users[this.getOtherUser()].cur_pet;
    },

    setCurUser(UserIndex){
        this.cur_user = UserIndex;
        this.m_users[this.cur_user].name.node.color = new cc.Color(255,0,0);
        this.m_users[this.getOtherUser()].name.node.color = new cc.Color(255,255,255);
    },

    updateMsg(){
        var next = true;
        for(var i = 0;i<this.m_users.length;++i){
            this.m_users[i].ui_blood.progress = this.m_users[i].cur_pet.cur_blood / this.m_users[i].cur_pet.Blood;
            if(this.m_users[i].cur_pet.cur_blood<=0){
                next = false;
                this.outMsg(this.m_users[i].cur_pet.name+"输了");
            }
        }
        return next;
    },

    closeSkillLists(){
        this.panel_skill.active = false;
    },

    outSkillLists(){
        this.panel_skill.active = true;
        for (var i = 0;i<this.ui_cur_skills.length;++i){
            this.ui_cur_skills[i].destroy();
        }
        this.ui_cur_skills.length = 0;
        for (var i = 0;i<this.m_users[this.cur_user].cur_pet.skillContent.length;++i){
            var s_skill = cc.instantiate(this.pre_ui_skill);
            this.ui_cur_skills.push(s_skill);
            s_skill.getComponent("skillTemplate").skillName = this.m_users[this.cur_user].cur_pet.skillContent[i].name;
            s_skill.getComponent("skillTemplate").skillSummary = this.m_users[this.cur_user].cur_pet.skillContent[i].summary;
            s_skill.getComponent("skillTemplate").skillIndex = i;
            s_skill.getComponent("skillTemplate").m_skill_content = this.panel_skillContent.getComponent("skillContent");
            this.panel_skill.addChild(s_skill);
        }
    },

    start () {
        this.read();
        this.loadPet(0,0);
        this.loadPet(0,1);
        this.setCurUser(0);
        this.outSkillLists();
        this.save();
    },

    // update (dt) {},
});
